/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.recover.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.richclient.application.support.AbstractView;
import reportcenter.recover.DBBackupExecutor;
import reportcenter.support.threading.JobListener;
import reportcenter.support.threading.WorkerJob;
import spin.Spin;

/**
 *
 * @author renan
 */
public class RecoverView extends AbstractView implements ApplicationListener, JobListener {

    @Override
    protected JComponent createControl() {
        JPanel view = new JPanel();
        view.setLayout(new BoxLayout(view, BoxLayout.Y_AXIS));

        JButton buttonBackup = new JButton(new BackupAction());
        view.add(buttonBackup);
        return view;
    }

    private class BackupAction extends AbstractAction {

        public BackupAction(){
            putValue(Action.NAME, "Efetuar Backup");
        }

        public void actionPerformed(ActionEvent e) {
            WorkerJob job = (WorkerJob) Spin.off(new DBBackupExecutor());

            // use the Spin.over functionality
            job.addJobListener((JobListener) Spin.over(RecoverView.this));

            // execute the job
            job.run();
        }
    }
    
    public void onApplicationEvent(ApplicationEvent arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void start() {
        getStatusBar().setMessage("Iniciando processo de geração do relatório..."); // And/or other Swing stuff
        getStatusBar().getProgressMonitor().taskStarted("Inicializando", -1);
    }

    public void end() {
//        getStatusBar().setMessage("Geração de relatório finalizada!"); // And/or other Swing stuff
        getStatusBar().getProgressMonitor().done();
    }

    public void progress(int percent) {
//        getStatusBar().setMessage(percent + "% completo"); // And/or other Swing stuff
        getStatusBar().getProgressMonitor().worked(percent);
    }

    public void status(String msg) {
        getStatusBar().setMessage(msg);
    }
}
